﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using NCC_SOP.app_codes;
using System.Web.Security;
using System.Collections;

namespace NCC_SOP.Admin.AdministratorOnly
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
                initBind();
            }
        }

        protected void initBind()
        {
            string roleName = (string)Session["RoleName"];
            RoleNameBox.Text = roleName;
            if (String.IsNullOrWhiteSpace(roleName))
            {
                Response.Redirect("~/Default.aspx",false);
            }
            try
            {
                dbConnection db = new dbConnection();
                string[] userNames = db.getAllUsers();
                Users.DataSource = userNames;
                Users.DataBind();

                string[] usersInRoles = db.getUsersInReportRole(Session["RoleName"].ToString());
                foreach (string user in usersInRoles)
                {
                    foreach (ListItem li in Users.Items)
                    {
                        if (user.Equals(li.Text))
                        {
                            li.Selected = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Session["ErrorMessage"] = e.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
        }

        protected void UpdateRole(object sender, EventArgs e)
        {
            try
            {
                dbConnection db = new dbConnection();
                string roleName = RoleNameBox.Text;
                string oldRoleName = Session["RoleName"].ToString();
                string selectSQL = "UPDATE dbo.rolesDef SET roleName = @roleName WHERE roleName = @oldRoleName";

                using (SqlConnection SqlConnection = new SqlConnection(db.connectionStr))
                {
                    SqlCommand cmd = new SqlCommand(selectSQL, SqlConnection);
                    cmd.Parameters.Add("@roleName", SqlDbType.VarChar);
                    cmd.Parameters.Add("@oldRoleName", SqlDbType.VarChar);
                    cmd.Parameters["@roleName"].Value = roleName;
                    cmd.Parameters["@oldRoleName"].Value = oldRoleName;
                    SqlConnection.Open();
                    cmd.ExecuteNonQuery();
                }

                //remove old roles
                string[] usersInRole = db.getUsersInReportRole(roleName);
                db.removeUsersFromReportRole(usersInRole, roleName);

                //add new roles
                string[] selectedUsers = retrieveFunctions.getSelectedItems(Users);
                db.addUsersIntoReportRole(selectedUsers, roleName);
                MultiView1.ActiveViewIndex = 1;
            }
            catch (Exception ee)
            {
                Session["ErrorMessage"] = ee.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
        }

        protected void RedirectToManageRoles(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdministratorOnly/manageRole.aspx");
        }
    }
}